/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import javax.swing.JOptionPane;

/**
 * CLase JFrame encargada de mostrar las opciones para borrar una rifa
 * @author Victoria Fuenmayor y Jose Gabriel Rivera
 */
public class JFrameBorrarRifa extends javax.swing.JFrame {

    private JPanelRifa rifa;
    private GUI gui;
    private JPanelCategoria panelCat;

    /**
     * Constructor. Inicializa los componentes del JFrame. Asigna asigna el atributo
     * GUI con el gui recibido. Crea un panel selector de categorías y un selector
     * de rifas. 
     * @param gui Recibe el gui principal
     */
    public JFrameBorrarRifa(final GUI gui) {
        initComponents();
        this.gui = gui;
        
        panelCat = new JPanelCategoria(gui.getCategoria(),gui){

            @Override
            public void evento() {
                if (panelCat.getNroCategorias()>0){
                    rifa.CargarRifas(panelCat.getIdCat());
                }            
            }
            
        };
        rifa = new JPanelRifa (gui.getCategoria(), gui){

            @Override
            public void eventoRifa() {
                if (rifa.getNroRifas()>0){
                    JOptionPane.showMessageDialog(this, "Advertencia: Borrará el contenido de la rifa seleccionada.");
                }else if (rifa.getIdRifa(panelCat.getIdCat())==-1){
                    return;
                }
            }
            
        };   
        this.add(panelCat);
        panelCat.setBounds(10, 20, 200, 22);
        this.add(rifa);
        rifa.setBounds(10, 60, 200, 22);
        this.setLocation(250, 200);
        this.setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jButton1.setText("Borrar");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton2)
                .addContainerGap(35, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(189, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * MouseClicked para el boton de borrar rifa. Si la rifa seleccionada es la rifa nula
     * muestra un mensaje de error, si la rifa es valida elimina de la base de datos la rifa
     * seleccionada y oculta todos los paneles en el GUI para poder actualizarlos.
     * @param evt 
     */
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        // TODO add your handling code here:
        if (panelCat.getIdCat()!=-1 && rifa.getIdRifa(panelCat.getIdCat())!=-1){
            ((logic.Categoria)(gui.getCategoria().getObject(panelCat.getIdCat()))).getRifas().removeObject(rifa.getIdRifa(panelCat.getIdCat()));
            gui.getPanelInfoRifa().setVisible(false);
            gui.getPanelBoletos().setVisible(false);
            gui.getBotonPagar().setVisible(false);
            gui.getjButtonComprar().setVisible(false);
            gui.getPanelInfoCliente().setVisible(false);
            gui.getListaRifa().setVisible(false);
            this.setVisible(false);
        }else{
            JOptionPane.showMessageDialog(this, "Error: Debe eleccione una rifa");
        }
    }//GEN-LAST:event_jButton1MouseClicked

    /**
     * MouseClicked para el boton cancelar. Si se presiona oculta la ventana.
     * @param evt 
     */
    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jButton2MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFrameBorrarRifa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFrameBorrarRifa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFrameBorrarRifa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFrameBorrarRifa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFrameBorrarRifa(new GUI("Raffle System")).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    // End of variables declaration//GEN-END:variables
}
